package com.example.keepmesafe.repository;

import java.io.File;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
	static final String LOG_TAG = "TagStorageImpl";
	static final String DATABASE_NAME = "kms.db";
	static final int DB_VERSION = 1;
	static final String TABLE = "tag";
	static final String C_ID = "_ID";
	static final String C_TAGNAME = "tagName";
	static final String C_TAGUSERNAME = "tagUsername";
	static final String C_TAGPASSWORD = "tagPasword";
	
	static final String TABLE_2 = "auth";
	static final String C_MASTER = "masterPassword";
	static final String C_EMAIL = "recoveryEmail";
	
	Context context;
	
	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DB_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE "+TABLE+" ( " + C_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ C_TAGNAME +" TEXT,"+ C_TAGUSERNAME +" TEXT,"+ C_TAGPASSWORD +" TEXT);");
		db.execSQL("CREATE TABLE "+TABLE_2+" ( " + C_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ C_MASTER +" TEXT,"+ C_EMAIL+" TEXT);");
		Log.d(LOG_TAG, "Created Tables "+TABLE+" "+TABLE_2);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS tag");
		db.execSQL("DROP TABLE IF EXISTS auth");
		onCreate(db);
	}

	public int deleteAllTags(SQLiteDatabase db){
		SQLiteDatabase.deleteDatabase(new File("/data/data/com.example.keepmesafe/databases/kms.db"));
		return db.delete(TABLE, null, null);
	}
	
	public int deleteTag(Long tagId,SQLiteDatabase db){
		Log.d(LOG_TAG, "Deleting Tag");
		String[] args={String.valueOf(tagId)};
		return db.delete(TABLE, C_ID+"=?", args);
	}
}
